/*                         Ralph Chess

		   rchess.cpp is a part of rchess

		 rchess is a very simple chess engine 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with rchess.  If not, see <http://www.gnu.org/licenses/>.

                   Copyright 2013 Ralph Glass                                              


         rchess.cpp - entry point and communication
*/

#include <iostream>
#include <fstream>
#include <sstream>
#include <signal.h>
#include "move.h"
#include "debug.h"

enum status { off = 0, initialized = 1, playing = 2 };
status engine;
std::ofstream debug_file;

void send(std::string message) { std::cout << message << std::endl; }

std::string read() {
	std::string command;
	std::cin >> command;
	return command;}

void log(std::string string) {
	debug_file << string << std::endl;
	debug_file.flush(); }

void process(std::string command) {
	std::string argument;
	std::string mov;

	log(command);

	if (command == "force") {
		//engine = off;
		init_engine();}

	if (command == "new" and engine == initialized) {
		init_engine();
		engine = playing;}

	if (command == "undo") {
		send("undo");}
	
	if ((command == "usermove") and engine == playing) { 
		argument = read();
		if (argument.length() == 4 or argument.length() == 5) {
			send("TimeLimit[0] = 0");
			send("TimeLimit[1] = 0");
			parse_usermove(argument);
			mov = get_move();
	        	log(argument);
			log(mov);
			send(mov);
			process(argument);
		} else return;}

	if (command == "show") {
		argument = read();}
		//if (argument == "board") {show_board( );}}

	if (command == "play") {
		send(get_move());}
		//show_board();};
				
	if (command == "protover") {                            
		std::cin >> argument;
		send("feature done=0");
		send("feature myname=\"Ralphs Chess\"");
		send("feature colors=0");
		send("feature pause=0");
		send("feature analyze=0");
		send("feature reuse=0");
		send("feature time=1");
		send("feature usermove=1");
		send("feature variants=\"normal\"");
		send("feature done=1"); 
		engine = initialized;};
	        
	if (command == "xboard") {
		send("TimeLimit[0] = 0");
		send("TimeLimit[1] = 0"); }}

void communication_with_xboard(int tid) { 
	std::string buffer_string;
	signal(SIGINT, SIG_IGN); 
	send("Chess");
	for (;;) {
		std::cin >> buffer_string;
		if (buffer_string == "quit") {return;};
		process(buffer_string);}}

int main() {
	init_engine();
	std::cout.setf(std::ios::unitbuf); //turn off buffering
	debug_file.open("debug.log");
	communication_with_xboard(1);
	debug_file.close();
	return 0;}
